﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using V5_DBUtility;
using V5_Model;

using System.Data.OleDb;
namespace V5_DAL.SqlServer {
    public class DALConfig  {

        #region  BaseMethod
        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId() {
            return DbHelperSQL.GetMaxID("ID", "K_Config");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from K_Config");
            strSql.Append(" where ID=" + ID);
            return DbHelperSQL.Exists(strSql.ToString());
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelConfig model) {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.ID != null) {
                strSql1.Append("ID,");
                strSql2.Append("" + model.ID + ",");
            }
            if (model.SiteTitle != null) {
                strSql1.Append("SiteTitle,");
                strSql2.Append("'" + model.SiteTitle + "',");
            }
            if (model.SiteUrl != null) {
                strSql1.Append("SiteUrl,");
                strSql2.Append("'" + model.SiteUrl + "',");
            }
            if (model.SiteLogo != null) {
                strSql1.Append("SiteLogo,");
                strSql2.Append("'" + model.SiteLogo + "',");
            }
            if (model.WebmasterName != null) {
                strSql1.Append("WebmasterName,");
                strSql2.Append("'" + model.WebmasterName + "',");
            }
            if (model.WebmasterEmail != null) {
                strSql1.Append("WebmasterEmail,");
                strSql2.Append("'" + model.WebmasterEmail + "',");
            }
            if (model.SiteCopyright != null) {
                strSql1.Append("SiteCopyright,");
                strSql2.Append("'" + model.SiteCopyright + "',");
            }
            if (model.SiteDescription != null) {
                strSql1.Append("SiteDescription,");
                strSql2.Append("'" + model.SiteDescription + "',");
            }
            if (model.SiteKeywords != null) {
                strSql1.Append("SiteKeywords,");
                strSql2.Append("'" + model.SiteKeywords + "',");
            }
            if (model.SiteState != null) {
                strSql1.Append("SiteState,");
                strSql2.Append("" + model.SiteState + ",");
            }
            if (model.SiteCloseText != null) {
                strSql1.Append("SiteCloseText,");
                strSql2.Append("'" + model.SiteCloseText + "',");
            }
            if (model.MailObject != null) {
                strSql1.Append("MailObject,");
                strSql2.Append("" + model.MailObject + ",");
            }
            if (model.MailServer != null) {
                strSql1.Append("MailServer,");
                strSql2.Append("'" + model.MailServer + "',");
            }
            if (model.MailServerUserName != null) {
                strSql1.Append("MailServerUserName,");
                strSql2.Append("'" + model.MailServerUserName + "',");
            }
            if (model.MailServerPassWord != null) {
                strSql1.Append("MailServerPassWord,");
                strSql2.Append("'" + model.MailServerPassWord + "',");
            }
            if (model.MailDomain != null) {
                strSql1.Append("MailDomain,");
                strSql2.Append("'" + model.MailDomain + "',");
            }
            if (model.TemplatePath != null) {
                strSql1.Append("TemplatePath,");
                strSql2.Append("'" + model.TemplatePath + "',");
            }
            if (model.UrlRewrite != null) {
                strSql1.Append("UrlRewrite,");
                strSql2.Append("'" + model.UrlRewrite + "',");
            }
            if (model.UploadType != null) {
                strSql1.Append("UploadType,");
                strSql2.Append("'" + model.UploadType + "',");
            }
            if (model.UploadMwidth != null) {
                strSql1.Append("UploadMwidth,");
                strSql2.Append("'" + model.UploadMwidth + "',");
            }
            if (model.UploadSwidth != null) {
                strSql1.Append("UploadSwidth,");
                strSql2.Append("'" + model.UploadSwidth + "',");
            }
            strSql.Append("insert into K_Config(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ModelConfig model) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update K_Config set ");
            if (model.SiteTitle != null) {
                strSql.Append("SiteTitle='" + model.SiteTitle + "',");
            }
            if (model.SiteUrl != null) {
                strSql.Append("SiteUrl='" + model.SiteUrl + "',");
            }
            if (model.SiteLogo != null) {
                strSql.Append("SiteLogo='" + model.SiteLogo + "',");
            }
            if (model.WebmasterName != null) {
                strSql.Append("WebmasterName='" + model.WebmasterName + "',");
            }
            if (model.WebmasterEmail != null) {
                strSql.Append("WebmasterEmail='" + model.WebmasterEmail + "',");
            }
            if (model.SiteCopyright != null) {
                strSql.Append("SiteCopyright='" + model.SiteCopyright + "',");
            }
            if (model.SiteDescription != null) {
                strSql.Append("SiteDescription='" + model.SiteDescription + "',");
            }
            if (model.SiteKeywords != null) {
                strSql.Append("SiteKeywords='" + model.SiteKeywords + "',");
            }
            if (model.SiteState != null) {
                strSql.Append("SiteState=" + model.SiteState + ",");
            }
            if (model.SiteCloseText != null) {
                strSql.Append("SiteCloseText='" + model.SiteCloseText + "',");
            }
            if (model.MailObject != null) {
                strSql.Append("MailObject=" + model.MailObject + ",");
            }
            if (model.MailServer != null) {
                strSql.Append("MailServer='" + model.MailServer + "',");
            }
            if (model.MailServerUserName != null) {
                strSql.Append("MailServerUserName='" + model.MailServerUserName + "',");
            }
            if (model.MailServerPassWord != null) {
                strSql.Append("MailServerPassWord='" + model.MailServerPassWord + "',");
            }
            if (model.MailDomain != null) {
                strSql.Append("MailDomain='" + model.MailDomain + "',");
            }
            if (model.TemplatePath != null) {
                strSql.Append("TemplatePath='" + model.TemplatePath + "',");
            }
            if (model.UrlRewrite != null) {
                strSql.Append("UrlRewrite='" + model.UrlRewrite + "',");
            }
            if (model.UploadType != null) {
                strSql.Append("UploadType='" + model.UploadType + "',");
            }
            if (model.UploadMwidth != null) {
                strSql.Append("UploadMwidth='" + model.UploadMwidth + "',");
            }
            if (model.UploadSwidth != null) {
                strSql.Append("UploadSwidth='" + model.UploadSwidth + "',");
            }

            if (model.Advertising != null)
            {
                strSql.Append("Advertising='" + model.Advertising + "',");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where ID=" + model.ID + " ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_Config ");
            strSql.Append(" where ID=" + ID);
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelConfig GetModel(int ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from K_Config ");
            strSql.Append(" where ID=" + ID);
            ModelConfig model = new ModelConfig();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "") {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SiteTitle"] != null && ds.Tables[0].Rows[0]["SiteTitle"].ToString() != "") {
                    model.SiteTitle = ds.Tables[0].Rows[0]["SiteTitle"].ToString();
                }
                if (ds.Tables[0].Rows[0]["SiteUrl"] != null && ds.Tables[0].Rows[0]["SiteUrl"].ToString() != "") {
                    model.SiteUrl = ds.Tables[0].Rows[0]["SiteUrl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["SiteLogo"] != null && ds.Tables[0].Rows[0]["SiteLogo"].ToString() != "") {
                    model.SiteLogo = ds.Tables[0].Rows[0]["SiteLogo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["WebmasterName"] != null && ds.Tables[0].Rows[0]["WebmasterName"].ToString() != "") {
                    model.WebmasterName = ds.Tables[0].Rows[0]["WebmasterName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["WebmasterEmail"] != null && ds.Tables[0].Rows[0]["WebmasterEmail"].ToString() != "") {
                    model.WebmasterEmail = ds.Tables[0].Rows[0]["WebmasterEmail"].ToString();
                }
                if (ds.Tables[0].Rows[0]["SiteCopyright"] != null && ds.Tables[0].Rows[0]["SiteCopyright"].ToString() != "") {
                    model.SiteCopyright = ds.Tables[0].Rows[0]["SiteCopyright"].ToString();
                }
                if (ds.Tables[0].Rows[0]["SiteDescription"] != null && ds.Tables[0].Rows[0]["SiteDescription"].ToString() != "") {
                    model.SiteDescription = ds.Tables[0].Rows[0]["SiteDescription"].ToString();
                }
                if (ds.Tables[0].Rows[0]["SiteKeywords"] != null && ds.Tables[0].Rows[0]["SiteKeywords"].ToString() != "") {
                    model.SiteKeywords = ds.Tables[0].Rows[0]["SiteKeywords"].ToString();
                }
                if (ds.Tables[0].Rows[0]["SiteState"] != null && ds.Tables[0].Rows[0]["SiteState"].ToString() != "") {
                    model.SiteState = int.Parse(ds.Tables[0].Rows[0]["SiteState"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SiteCloseText"] != null && ds.Tables[0].Rows[0]["SiteCloseText"].ToString() != "") {
                    model.SiteCloseText = ds.Tables[0].Rows[0]["SiteCloseText"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MailObject"] != null && ds.Tables[0].Rows[0]["MailObject"].ToString() != "") {
                    model.MailObject = int.Parse(ds.Tables[0].Rows[0]["MailObject"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MailServer"] != null && ds.Tables[0].Rows[0]["MailServer"].ToString() != "") {
                    model.MailServer = ds.Tables[0].Rows[0]["MailServer"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MailServerUserName"] != null && ds.Tables[0].Rows[0]["MailServerUserName"].ToString() != "") {
                    model.MailServerUserName = ds.Tables[0].Rows[0]["MailServerUserName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MailServerPassWord"] != null && ds.Tables[0].Rows[0]["MailServerPassWord"].ToString() != "") {
                    model.MailServerPassWord = ds.Tables[0].Rows[0]["MailServerPassWord"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MailDomain"] != null && ds.Tables[0].Rows[0]["MailDomain"].ToString() != "") {
                    model.MailDomain = ds.Tables[0].Rows[0]["MailDomain"].ToString();
                }
                if (ds.Tables[0].Rows[0]["TemplatePath"] != null && ds.Tables[0].Rows[0]["TemplatePath"].ToString() != "") {
                    model.TemplatePath = ds.Tables[0].Rows[0]["TemplatePath"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UrlRewrite"] != null && ds.Tables[0].Rows[0]["UrlRewrite"].ToString() != "") {
                    model.UrlRewrite = ds.Tables[0].Rows[0]["UrlRewrite"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UploadType"] != null && ds.Tables[0].Rows[0]["UploadType"].ToString() != "") {
                    model.UploadType = ds.Tables[0].Rows[0]["UploadType"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UploadMwidth"] != null && ds.Tables[0].Rows[0]["UploadMwidth"].ToString() != "") {
                    model.UploadMwidth = ds.Tables[0].Rows[0]["UploadMwidth"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UploadSwidth"] != null && ds.Tables[0].Rows[0]["UploadSwidth"].ToString() != "") {
                    model.UploadSwidth = ds.Tables[0].Rows[0]["UploadSwidth"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Advertising"] != null && ds.Tables[0].Rows[0]["Advertising"].ToString() != "")
                {
                    model.Advertising = ds.Tables[0].Rows[0]["Advertising"].ToString();
                }
                return model;
            }
            else {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM K_Config ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where  1=1 " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion  Method

        #region K_Config开始
        public bool DeleteList(string IDlist) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_Config ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder) {
            return this.GetList("*", Top, strWhere, filedOrder);
        }

        public DataSet GetList(int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            return this.GetList("*", PageSize, PageIndex, strWhere, strOrder, ref RecordCount);
        }

        public List<ModelConfig> GetModelList(string strWhere) {
            DataSet ds = this.GetList(strWhere);
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                return DBCommonHelper.FillModel<ModelConfig>(ds);
            }
            return null;
        }

        public List<ModelConfig> GetModelList(int Top, string strWhere, string filedOrder) {
            DataSet ds = this.GetList(Top, strWhere, filedOrder);
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                return DBCommonHelper.FillModel<ModelConfig>(ds);
            }
            return null;
        }

        public List<ModelConfig> GetModelList(int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            DataSet ds = this.GetList(PageSize, PageIndex, strWhere, strOrder, ref RecordCount);
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                return DBCommonHelper.FillModel<ModelConfig>(ds);
            }
            return null;
        }

        public DataSet GetList(string strFiled, string strWhere) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select " + strFiled + " ");
            strSql.Append(" FROM K_Config ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where 1=1 " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetList(string strFiled, int Top, string strWhere, string filedOrder) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0) {
                strSql.Append(" Top " + Top.ToString());
            }
            strSql.Append(" " + strFiled + " ");
            strSql.Append(" FROM K_Config ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where 1=1 " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetList(string strFiled, int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            StringBuilder strSqlCount = new StringBuilder();
            strSqlCount.Append("Select Count(*) as ID From K_Config ");
            if (strWhere.Trim() != "") {
                strSqlCount.Append(" where 1=1 " + strWhere);
            }
            RecordCount = Convert.ToInt32("0" + DbHelperSQL.GetSingle(strSqlCount.ToString()));
            if (PageIndex <= 1) {
                PageIndex = 1;
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Top " + PageSize + " " + strFiled + " from K_Config where 1=1 ");
            if (strWhere.Trim() != "") {
                strSql.Append(" " + strWhere);
            }
            if (PageIndex > 1) {
                strSql.Append(" And ID Not In (Select Top " + ((PageIndex - 1) * PageSize) + " ID From K_Config Where 1=1 ");
                if (strWhere.Trim() != "") {
                    strSql.Append(" " + strWhere);
                }
                if (!string.IsNullOrEmpty(strOrder)) {
                    strSql.Append(" Order by " + strOrder);
                }
                strSql.Append(")");
            }
            if (!string.IsNullOrEmpty(strOrder)) {
                strSql.Append(" Order by " + strOrder);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion K_Config结束
    
    


    }
}
